package com.example.schedulemanager.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.schedulemanager.R;
import com.example.schedulemanager.model.Schedule;

import java.util.ArrayList;
import java.util.List;

public class ScheduleAdapter extends RecyclerView.Adapter<ScheduleAdapter.ScheduleViewHolder> {
    
    private List<Schedule> schedules = new ArrayList<>();
    private OnItemClickListener listener;
    
    public interface OnItemClickListener {
        void onItemClick(Schedule schedule);
        void onDeleteClick(Schedule schedule);
        void onCompletionToggle(Schedule schedule);
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public ScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.schedule_item, parent, false);
        return new ScheduleViewHolder(itemView);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ScheduleViewHolder holder, int position) {
        Schedule currentSchedule = schedules.get(position);
        holder.titleTextView.setText(currentSchedule.getTitle());
        holder.timeTextView.setText(currentSchedule.getTime());
        holder.descriptionTextView.setText(currentSchedule.getDescription());
        
        if (currentSchedule.isCompleted()) {
            holder.titleTextView.setAlpha(0.5f);
            holder.timeTextView.setAlpha(0.5f);
            holder.descriptionTextView.setAlpha(0.5f);
            holder.itemView.setBackgroundResource(R.drawable.completed_schedule_bg);
        } else {
            holder.titleTextView.setAlpha(1.0f);
            holder.timeTextView.setAlpha(1.0f);
            holder.descriptionTextView.setAlpha(1.0f);
            holder.itemView.setBackgroundResource(R.drawable.schedule_item_bg);
        }
    }
    
    @Override
    public int getItemCount() {
        return schedules.size();
    }
    
    public void setSchedules(List<Schedule> schedules) {
        this.schedules = schedules;
        notifyDataSetChanged();
    }
    
    class ScheduleViewHolder extends RecyclerView.ViewHolder {
        private TextView titleTextView;
        private TextView timeTextView;
        private TextView descriptionTextView;
        private ImageButton deleteButton;
        
        public ScheduleViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.titleTextView);
            timeTextView = itemView.findViewById(R.id.timeTextView);
            descriptionTextView = itemView.findViewById(R.id.descriptionTextView);
            deleteButton = itemView.findViewById(R.id.deleteButton);
            
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (listener != null && position != RecyclerView.NO_POSITION) {
                    listener.onCompletionToggle(schedules.get(position));
                }
            });
            
            itemView.setOnLongClickListener(v -> {
                int position = getAdapterPosition();
                if (listener != null && position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(schedules.get(position));
                    return true;
                }
                return false;
            });
            
            deleteButton.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (listener != null && position != RecyclerView.NO_POSITION) {
                    listener.onDeleteClick(schedules.get(position));
                }
            });
        }
    }
} 